{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "65b8b4ee",
   "metadata": {},
   "source": [
    "# 人脸识别实践——宿舍智能识别门禁系统（xxx栋）\n",
    "\n",
    "* 1. 创建FaceSet\n",
    "* 2. 查询FaceSet\n",
    "* 3. 存入face_token数据\n",
    "* 4. 获取FaceSet信息数据\n",
    "* 5. 人脸搜索\n",
    "* 6. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c685ec8",
   "metadata": {},
   "source": [
    " ## 1. 创建FaceSet"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50c5d00c",
   "metadata": {},
   "source": [
    "* 描述\n",
    "> 1. 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。\n",
    "> 2. 试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet 请求参数\n",
    "\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "\n",
    "* 调用方法\n",
    "> POST\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e3c850ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "07fd2461",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&amp;=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&amp;=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25153f05",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "67c4e5d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7b64d55a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_Create_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = FaceSet_Create_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f503d6a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'aedc2b2026d703dc5df99d80dca79676',\n",
       " 'time_used': 148,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1649058271,0ddd0767-cc03-493f-93f8-c906b41569ea',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "971086a0",
   "metadata": {},
   "source": [
    "## 2. 查询FaceSet"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06c81b92",
   "metadata": {},
   "source": [
    "* 描述\n",
    "> 1. 获取某一 API Key 下的 FaceSet 列表及其 faceset_token、outer_id、display_name 和 tags 等信息。\n",
    "> 2.注意：2017年8月16日后，调用本接口将不会一次性返回全量的 FaceSet。单次查询最多返回 100 个 FaceSet。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序。\n",
    "\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\n",
    "\n",
    "* 调用方法\n",
    "> POST\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f2e936c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>包含需要查询的FaceSet标签的字符串，用逗号分隔</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>start</td>\n",
       "      <td>Int</td>\n",
       "      <td>一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选         参数名      类型                                               参数说明\n",
       "0   必选     api_key  String                                     调用此API的API Key\n",
       "1   必选  api_secret  String                                  调用此API的API Secret\n",
       "2   可选        tags  String                         包含需要查询的FaceSet标签的字符串，用逗号分隔\n",
       "3   可选       start     Int  一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号..."
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Secret</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">包含需要查询的FaceSet标签的字符串，用逗号分隔</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">start</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号。</span></p><p><span style=\"color: rgb(0,0,0);\">通过传入数字 n，可以控制本 API 从第 n 个 faceset_token 开始返回。返回的 faceset_token 按照创建时间排序。每次返回<span class=\"s1\">1000</span>个<span class=\"s1\">FaceSets</span>。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">默认值为1。</span></p><p class=\"p1\"><span style=\"color: rgb(255,0,0);\">注意：2017年8月16日后，每次返回的 FaceSet 数量上限从 1000 改为 100。并且您可以输入上一次请求本 API 返回的 next 值，用以获得接下来的 100 个 faceset_token。请尽快修改调整您的程序。</span></p></td></tr></tbody></table>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9405acdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_getfacesets_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = FaceSet_getfacesets_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "918cacba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 71,\n",
       " 'facesets': [{'faceset_token': 'd0637b914ec7c1a80af356fac6bdf684',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '81017929c3296dd18a8cd9d5013b21ac',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '62e054b143384164b1f864bdc6ee867a',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '0e24c0a3460625e345000bbd0150552b',\n",
       "   'outer_id': 'NFUdaobi',\n",
       "   'display_name': '广州南方学院西2宿舍楼',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'fe7e0b98d16c03cbfe0f94e610322eef',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'aedc2b2026d703dc5df99d80dca79676',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1649058273,4280e652-58c7-4296-a9f2-f074f0737406'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "346c78de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'aedc2b2026d703dc5df99d80dca79676'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['facesets'][-1]['faceset_token'] # 取值\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50094a8a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9eaa0715",
   "metadata": {},
   "source": [
    "## 更新人脸库信息API"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "711d1459",
   "metadata": {},
   "source": [
    "* 描述\n",
    "> 更新一个人脸集合的属性\n",
    "\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/update\n",
    "\n",
    "* 调用方法\n",
    "> POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "2bc804ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'display_name':'广州南方学院西2宿舍楼',\n",
    "    'new_outer_id':'NFUdaobi',\n",
    "    'user_data':'为什么没有字为什么没有字'\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = FaceSet_update_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "a5b68c2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 52,\n",
       " 'error_message': 'NEW_OUTER_ID_EXIST',\n",
       " 'request_id': '1649058274,ed95219e-4f60-430d-84de-2273614eb927'}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afe682c2",
   "metadata": {},
   "source": [
    "## 4. 检查数据集合信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c49cc238",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_getdetail_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'outer_id':'NFUdaobi'\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = FaceSet_getdetail_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f2bbec28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '0e24c0a3460625e345000bbd0150552b',\n",
       " 'tags': '',\n",
       " 'time_used': 75,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院西2宿舍楼',\n",
       " 'face_tokens': ['1e86759f79ca14ce993fc94770542c3b'],\n",
       " 'face_count': 1,\n",
       " 'request_id': '1649058274,54dabaeb-0b13-436a-9d65-8260c7b19dea',\n",
       " 'outer_id': 'NFUdaobi'}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2dad162",
   "metadata": {},
   "source": [
    "## 5. 人脸检测，获取人脸token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "31a31034",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "eaa5d17a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    e:\\2021-2022.1\\api，机器学习与人工智能\\git\\week04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "517f3d92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649059033,c9d0f73e-7f69-4e43-ae1a-baeb208399f8',\n",
       " 'time_used': 94,\n",
       " 'faces': [{'face_token': 'ae0401e32708345391a9cdbdea11b566',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fae992cd",
   "metadata": {},
   "source": [
    "## 6. 人脸存放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "0ce7e619",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_addface_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'nszWQiTxf4n1D3-O39QvSqlqdaaj8KET'\n",
    "API_sercret = '48QuBUsMozhv02SL_v2UaZkeyEt6W4Nq'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'outer_id':'NFUdaobi',\n",
    "    'face_tokens':'1e86759f79ca14ce993fc94770542c3b'\n",
    "}\n",
    "\n",
    "# 5. 发起请求requests\n",
    "r = requests.post(url = FaceSet_addface_url , params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "15bb5e3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '0e24c0a3460625e345000bbd0150552b',\n",
       " 'time_used': 112,\n",
       " 'face_count': 1,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1649059494,ecdc9f52-7558-4ce6-a0e3-2cb9aeca649f',\n",
       " 'outer_id': 'NFUdaobi',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2a0cb5f",
   "metadata": {},
   "source": [
    "# 实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "6d109534",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "e85b7ce7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "face_compare() takes 3 positional arguments but 4 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-119-68ce8a4a8b87>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mcompare\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mface_compare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_sercret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'Liu_01.jpeg'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'lg.jpg'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: face_compare() takes 3 positional arguments but 4 were given"
     ]
    }
   ],
   "source": [
    "compare.face_compare(API_key,API_sercret,'Liu_01.jpeg','lg.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94276b9b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "273aedbd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "61456c71",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "3648eed4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 68,\n",
       " 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id',\n",
       " 'request_id': '1649059824,a5dbd09e-3bbf-48ce-bf9f-80dbac9fdacd'}"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "2c8e6dcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "32c03309",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 50,\n",
       " 'error_message': 'VOID_REQUEST',\n",
       " 'request_id': '1649060038,43d46eab-931c-42fd-a4af-1a1db55b73ec'}"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52e0b5a5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
